﻿using System;
using System.Windows;
using System.Windows.Controls;

using SBPweb.PROvibe.Client.Framework.Contracts.Content;

namespace SBPweb.PROvibe.Client.Framework.Entry
{

    public partial class RootVisual : UserControl
    {

        private IPage lastPage;
        private bool isShown = false;
        private bool isLoadShowing = false;
        private bool isLoadHiding = false;

        public RootVisual()
        {
            InitializeComponent();
        }

        public void ShowPage(IPage page)
        {
            UIElement p = page as UIElement;
            //if (lastPage != null)
            //{
            //    lastPage.Hide();
            //}
            this.Content.Children.Add(p);
            lastPage = page;
        }

        public void ShowLoaderSplash()
        {
            lock (this)
            {
                if (isLoadShowing || isShown)
                {
                    return;
                }
                LoaderSplash.Visibility = Visibility.Visible;
                isLoadShowing = true;
                Utilities.Animations.StoryboardHelper.BeginElementStoryBoard(Resources, LoaderSplash, "LoaderShow", new PropertyPath("Opacity"), new EventHandler(LoaderSplashShowCompleted));
            }
        }

        public void HideLoaderSplash()
        {
            lock (this)
            {
                if (isLoadHiding)
                {
                    return;
                }
                isLoadHiding = true;
                Utilities.Animations.StoryboardHelper.BeginElementStoryBoard(Resources, LoaderSplash, "LoaderHide", new PropertyPath("Opacity"), new EventHandler(LoaderSplashHideCompleted));
            }
        }

        private void LoaderSplashShowCompleted(object sender, EventArgs e)
        {
            lock (this)
            {
                isLoadShowing = false;
                isShown = true;
            }
        }

        private void LoaderSplashHideCompleted(object sender, EventArgs e)
        {
            lock (this)
            {
                isLoadHiding = false;
                isShown = false;
                LoaderSplash.Visibility = Visibility.Collapsed;
            }
        }

    }

}